package com.itheima.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.pojo.Shopping;
import com.itheima.pojo.ShoppingDTO;
import com.itheima.service.ShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sku")
public class ShoppingController {

    @Autowired
    private ShoppingService shoppingService;

    @PostMapping()
    public String save (@RequestBody Shopping shopping){
       shoppingService.save(shopping);

       return "添加成功";
    }


    @GetMapping("/{skuId}")
    public Shopping getById(@PathVariable Long skuId){

       Shopping list = shoppingService.getById(skuId);

        return list;
    }

    @PostMapping("/{pageNum}/{pageSize}")
    public PageInfo<Shopping> page(@PathVariable Integer pageNum, @PathVariable Integer pageSize,@RequestBody ShoppingDTO shoppingDTO){

        PageHelper.startPage(pageNum, pageSize);

       List<Shopping> list =  shoppingService.list(pageNum,pageSize,shoppingDTO);

        return new PageInfo<>(list);
    }

    @DeleteMapping("/{skuId}")
    public String delect(@PathVariable Long skuId){
        shoppingService.delect(skuId);
        return "删除成功";
    }


    @PutMapping
    public String updete(@RequestBody Shopping shopping){
        shoppingService.updete(shopping);
        return "修改成功";
    }





}


